package com.sanshow.charging.fragment;

import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;

import com.sanshow.charging.MainActivity;
import com.sanshow.charging.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VideoFragmentbak extends BaseFragment implements MediaPlayer.OnCompletionListener {

    public VideoView videoView;
    public ImageView imageView;
    public MediaPlayer mediaPlayer;
    private Bitmap bitmap;
    public List<String> allDataFileName;
    private static int i = 0; // 播放索引
    private static String DOWNLOADPATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/sanshow/videos/";

    public VideoFragmentbak() {
    }

    public static VideoFragmentbak newInstance() {
        VideoFragmentbak fragment = new VideoFragmentbak();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_video, container, false);

        //videoView = view.findViewById(R.id.videoView);

        initVideo();
        return view;
    }

    private void initVideo() {
        Log.d("testddd", getAllDataFileName().toString());
        MainActivity mainActivity = (MainActivity) getActivity();


        int i = 0;
        allDataFileName = getAllDataFileName();
        if (allDataFileName.size() > 0) {
            // 本地数据
            Uri videoUri = Uri.parse(allDataFileName.get(0));
            videoView.setVideoURI(videoUri);
            videoView.start();
            videoView.setOnCompletionListener(this);
        } else {
            // 默认项目内视频
            //final Uri videoUri = Uri.parse("android.resource://" + mainActivity.getPackageName() + "/" + R.raw.logo);
            //videoView.setVideoURI(videoUri);
            videoView.start();

            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    //videoView.setVideoURI(videoUri);
                    videoView.start();
                }
            });

            videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    if (what == 100)
                    {
                        //Toast.makeText(getActivity(),"视频播放错误提示",Toast.LENGTH_LONG).show();
                        //videoView.stopPlayback();
                        //videoView.resume();
                        videoView.stopPlayback();
                        videoView.start();
                    }
                    return true;
                }
            });
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (allDataFileName.size() == 1) {
            videoView.setVideoURI(Uri.parse(allDataFileName.get(0)));
            videoView.start();
        } else {
            videoView.setVideoURI(Uri.parse(allDataFileName.get(i)));
            videoView.start();
            i++;
            if (i == allDataFileName.size()) {
                i = 0;
            }
        }
    }

    // 获取目录下的所有文件夹
    public List<String> getAllDataFileName() {
        List<String> fileList = new ArrayList<>();

        File file = new File(DOWNLOADPATH);
        File[] tempList = file.listFiles();
        if (tempList != null && tempList.length > 0) {
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isFile()) {
                    System.out.println("文     件：" + tempList[i].getName());
                    // tempList[i].toString();// 路径
                    // tempList[i].getName();// 文件名
                    // 文件名
                    String fileName = DOWNLOADPATH + tempList[i].getName();
                    fileList.add(fileName);
                }
            }
        }
        return fileList;
    }

}